﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exentials.Xoox.Spreadsheet
{
    public struct Unit
    {
        private double _value;
        private UnitType _unitType;

        internal Unit(double value, UnitType unitType)
        {
            switch (unitType)
            {
                case UnitType.CM: _value = Math.Round(value / 2.54, 1); break;
                default: _value = Math.Round(value, 1); break;
            }

            _unitType = unitType;
        }

        public double GetValue()
        {
            return GetValue(_unitType);
        }

        public double GetValue(UnitType unitType)
        {
            switch (unitType)
            {
                case UnitType.CM: return Math.Round(_value * 2.54, 1);
                default: return _value;
            }
        }

        public UnitType UnitType
        {
            get { return _unitType; }
        }

        public override string ToString()
        {
            return _value.ToString(System.Globalization.CultureInfo.InvariantCulture);
        }

        public static implicit operator Unit(double value)
        {
            return new Unit(value, UnitType.Inch);
        }

        public static Unit Define(double value, UnitType unitType)
        {
            return new Unit(value, unitType);
        }
    }
}
